package models

import (
	"doctor-srv/basic/config"
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	PatientId   int    `json:"patient_id" gorm:"type:int;comment:'患者id'"`
	DoctorId    int    `json:"doctor_id" gorm:"type:int;comment:'医生id'"`
	RecordId    int    `json:"record_id" gorm:"type:int;comment:'问诊id'"`
	PrescriptId int    `json:"prescript_id" gorm:"type:int;comment:'处方id'"`
	OrderSn     string `json:"order_sn" gorm:"type:varchar(50);comment:'订单编号'"`
	Price       string `json:"price" gorm:"type:varchar(30);comment:'订单金额'"`
	PayUrl      string `json:"pay_url" gorm:"type:varchar(300);comment:'支付链接'"`
	PayMothed   int    `json:"pay_mothed" gorm:"type:int;comment:'支付方式 1-支付宝 2-微信'"`
	PayStatus   int    `json:"pay_status" gorm:"type:int;comment:'支付状态 1-待支付 2-未付款交易超时 3-交易支付成功 4-交易结束'"`
}

func (o *Order) TableName() string {
	return "order"
}

func (o *Order) CreateOrder() error {
	return config.DB.Create(&o).Error
}

func (o *Order) GetOrder(id int) error {
	return config.DB.Model(&Order{}).Where("id = ?", id).Limit(1).Find(&o).Error
}

func (o *Order) UpdateStatus(orderSn string) error {
	return config.DB.Model(&Order{}).Where("order_sn = ?", orderSn).Updates(&o).Find(&o).Error
}
